package com.iraeta.service.iraorder.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iraeta.dao.iraorder.IrascstepmodelDao;
import com.iraeta.dao.iraorder.IrascstepmodelmxDao;
import com.iraeta.entity.Irascstepmodel;
import com.iraeta.entity.Irascstepmodelmx;
import com.iraeta.service.iraorder.IraScStepModelService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class IraScStepModelServiceImpl implements IraScStepModelService {
    @Resource
    IrascstepmodelDao irascstepmodelDao;
    @Resource
    IrascstepmodelmxDao irascstepmodelmxDao;

    @Transactional
    @Override
    public void addIraScStepModel(Irascstepmodel iraScStepModel, JSONArray modelMxJSONArray) {
        irascstepmodelDao.insert(iraScStepModel);
        for(int i=0;i<modelMxJSONArray.size();i++){
            JSONObject mxJSONObject = modelMxJSONArray.getJSONObject(i);
            Irascstepmodelmx irascstepmodelmx = new Irascstepmodelmx();
            irascstepmodelmx.setModelid(iraScStepModel.getId());
            irascstepmodelmx.setStepname(StrUtil.isEmpty(mxJSONObject.getString("stepname"))?"step" : mxJSONObject.getString("stepname"));
            irascstepmodelmx.setSortid(mxJSONObject.getIntValue("sortid"));
            this.irascstepmodelmxDao.insert(irascstepmodelmx);
        }
    }

    @Transactional
    @Override
    public void updateIraScStepModel(Irascstepmodel iraScStepModel, JSONArray modelMxJSONArray) {
        irascstepmodelDao.updateByPrimaryKey(iraScStepModel);
        for(int i=0;i<modelMxJSONArray.size();i++){
            JSONObject mxJSONObject = modelMxJSONArray.getJSONObject(i);
            int id = mxJSONObject.getIntValue("id");
            boolean isUpdate = id > 0;
            Irascstepmodelmx irascstepmodelmx = isUpdate ? irascstepmodelmxDao.selectByPrimaryKey(id) : new Irascstepmodelmx();
            if(isUpdate){
                irascstepmodelmx.setStepname(StrUtil.isEmpty(mxJSONObject.getString("stepname"))?"step" : mxJSONObject.getString("stepname"));
                this.irascstepmodelmxDao.updateByPrimaryKey(irascstepmodelmx);
            }else{
                irascstepmodelmx.setModelid(iraScStepModel.getId());
                irascstepmodelmx.setStepname(StrUtil.isEmpty(mxJSONObject.getString("stepname"))?"step" : mxJSONObject.getString("stepname"));
                irascstepmodelmx.setSortid(mxJSONObject.getIntValue("sortid"));
                this.irascstepmodelmxDao.insert(irascstepmodelmx);
            }
        }
    }

    @Transactional
    @Override
    public void deleteIraScStepModel(Integer id) {
        irascstepmodelDao.deleteByPrimaryKey(id);
        List<Irascstepmodelmx> irascstepmodelmxList = irascstepmodelmxDao.selectByModelId(id);
        for (Irascstepmodelmx irascstepmodelmx : irascstepmodelmxList) {
            irascstepmodelmxDao.deleteByPrimaryKey(irascstepmodelmx.getId());
        }
    }

    @Override
    public Irascstepmodel getIraScStepModel(Integer id) {
        return irascstepmodelDao.selectByPrimaryKey(id);
    }

    @Override
    public List<Irascstepmodel> listIraScStepModel() {
        return irascstepmodelDao.selectAll();
    }

    @Override
    public void deleteIraScStepModelMx(Integer id) {
        irascstepmodelmxDao.deleteByPrimaryKey(id);
    }

    @Override
    public List<Irascstepmodelmx> listIraScStepModelMx(Integer modelId) {
        return irascstepmodelmxDao.selectByModelId(modelId);
    }
}
